if (!rialto.widgetBehavior) rialto.widgetBehavior = {};

/* little user (dvlper) guide :
to create <Bhv> behavior as <baseBhv> extension , use this idiom :
rialto.widgetBehavior.createBehavior(
	'Bhv',
	(function (setup) {
		// code specif Bhv behavior
		this.atrBhv = setup.spec2;
	}
	),
	'baseBhv'	
);

and to affect a object <o2> with this behavior <Bhv> 
and <setup> as behaviors modalities:
	var o2 = {atr1:"at1", atr2:"at2"}
	var setup = {spec1:"sp1", spec2:"sp2"};

	rialto.widgetBehavior.affect(o2,'Bhv',setup);
*/


rialto.widgetBehavior.createBehavior = function (behavior,baseFunction,extendedBehavior) {
//----------------------------------
	rialto.widgetBehavior[behavior] = function (setup) {

/* traceExec('behavior= '+behavior+' setup.bRectLim= '+setup.bRectLim,73); */
		//or :
		//this.firstAffect = rialto.widgetBehavior.$$coreAffect;
		//if (!this.firstAffect(behavior,setup)) return;
		if (! rialto.widgetBehavior.$$coreAffect.apply(this, [behavior, setup])) 
			return;
		
		//or :
		//this.body = functionBody;
		//this.body(setup);
		baseFunction.apply(this,[setup]);
		
		if (baseFunction.prototype) 
			for (var proto in baseFunction.prototype) {				
				if (typeof baseFunction.prototype[proto] == 'function') 
					this[proto] = baseFunction.prototype[proto];
				
			}

		
	}

	if (baseFunction.prototype) 
		rialto.widgetBehavior[behavior].prototype = {};
		for (var proto in baseFunction.prototype) {				
			if (typeof baseFunction.prototype[proto] != 'function') 
				rialto.widgetBehavior[behavior].prototype[proto] = 
							baseFunction.prototype[proto];
			
		}
		
	if (extendedBehavior)
			rialto.widgetBehavior[behavior].superClass = rialto.widgetBehavior[extendedBehavior];
}

rialto.widgetBehavior.$$coreAffect = function (behavior,setup) {
//--------------------------
/* traceExec('$$coreAffect setup.bRectLim= '+setup.bRectLim,73); */
	if (!this.withBehavior) this.withBehavior = {};
	if (typeof this.withBehavior[behavior] != 'undefined') {
		//alert('!= undef this.withBehavior['+behavior+'] = '+this.withBehavior[behavior]);
		return false;
	}
	else {
		if (rialto.widgetBehavior[behavior].superClass) {
			
			this.base = rialto.widgetBehavior[behavior].superClass; // this.constructor.prototype;  //.base;
			this.base(setup);
		}
		//alert('== undef withBehavior['+behavior+']= '+this.withBehavior[behavior]);
		this.withBehavior[behavior] = true;
		return true;
	}	
}

rialto.widgetBehavior.affect = function(widget,behavior,setup) {
//--------------------------
	if (!setup) setup = {};
	/* traceExec('** affect '+behavior+' enter widget.id= '+(widget.id?widget.id:'?')+ 'setup.bRectLim= '+setup.bRectLim,73); */
	if ((!widget.withBehavior) || widget.withBehavior[behavior] == undefined) {
		widget[behavior] = rialto.widgetBehavior[behavior];
		widget[behavior](setup);
		/* traceExec('** affect nv bhv maded',73); */
	}
	else
		rialto.widgetBehavior.modify(widget,behavior,setup);
	
}

rialto.widgetBehavior.modify = function(widget,behavior,setup) {
//--------------------------
	alert('a implementer : doit realiser une modification de comportement'); 
	// il faut pr?voir dsz les methodes modify la surcharge diff?rentielle des specs 
}


/* By way of user guide :
//------------ use of rialto.widgetBehavior.createBehavior method for create behaviors 

var fctDD = function (setup) {
		// code specif DragAndDrop behavior
		this.atrSupl1 = setup.spec1;
	};

rialto.widgetBehavior.createBehavior(
	'DragAndDrop',
	fctDD
);


rialto.widgetBehavior.createBehavior(
	'Missile',
	(function (setup) {
		// code specif Missile behavior
		this.atrMissile = setup.spec2;
	}
	),
	'DragAndDrop'	
);

rialto.widgetBehavior.createBehavior(
	'SophisticatedMissile',
	(function (setup) {
		// code specif SophisticatedMissile behavior
		this.atrSophisticatedMissile = setup.spec3;
	}
	),
	'Missile'	
);



*/
//--------  alternative (older) declaration  behavior function
/*
rialto.widgetBehavior.DragAndDrop = function (setup) {
//-------------------------------

	// common code behavior
	this.coreBehavior = rialto.widgetBehavior.$$coreAffect;
	if (this.coreBehavior('DragAndDrop',setup)) return;

	// code specif DragAndDrop behavior
	this.atrSupl1 = setup.spec1;
}
rialto.widgetBehavior.Missile = function (setup) {
//---------------------------
	// common code behavior
	this.coreBehavior = rialto.widgetBehavior.$$coreAffect;
	if (this.coreBehavior('Missile',setup)) return;

	// code specif Missile behavior
	this.atrMissile = setup.spec2;
}
rialto.widgetBehavior.Missile.superClass = rialto.widgetBehavior.DragAndDrop;

rialto.widgetBehavior.SophisticatedMissile = function (setup) {
//----------------------------------------
alert('SophisticatedMissile constr');
	// common code behavior
	this.coreBehavior = rialto.widgetBehavior.$$coreAffect;
	if (this.coreBehavior('SophisticatedMissile',setup)) return;

	// code specif DragAndDrop behavior
	this.atrSophisticatedMissile = setup.spec3;
}
rialto.widgetBehavior.SophisticatedMissile.superClass = rialto.widgetBehavior.Missile;
*/
/*
//------------ tests
function test() {
//----- test 1
	var o = {atr1:"at1", atr2:"at2"}
	var setup = {spec1:"sp1", spec2:"sp2", spec3:"sp3"};

	rialto.widgetBehavior.affect(o,'DragAndDrop',setup);
	alert(
	'o.atr1= '+o.atr1
	+' o.atrSupl1 = '+o.atrSupl1
	);

	rialto.widgetBehavior.affect(o,'Missile',setup);
	alert(
	'o.atr1= '+o.atr1
	+' o.atrSupl1 = '+o.atrSupl1
	+' o.atrMissile = '+o.atrMissile
	);
	
	rialto.widgetBehavior.affect(o,'SophisticatedMissile',setup);
	alert(
	'o.atr1= '+o.atr1
	+' o.atrSupl1 = '+o.atrSupl1
	+' o.atrMissile = '+o.atrMissile
	+' o.atrSophisticatedMissile = '+o.atrSophisticatedMissile
	);
	

//---- test 2	
	var o2 = {atr1:"at1", atr2:"at2"}
	var setup = {spec1:"sp1", spec2:"sp2"};

	rialto.widgetBehavior.affect(o2,'Missile',setup);
	
	alert(
	'o2.atr1= '+o2.atr1
	+' o2.atrSupl1 = '+o2.atrSupl1
	+' o2.atrMissile = '+o2.atrMissile);

	rialto.widgetBehavior.affect(o2,'DragAndDrop',setup);
	alert(
	'o2.atr1= '+o2.atr1
	+' o2.atrSupl1 = '+o2.atrSupl1
	+' o2.atrMissile = '+o2.atrMissile);

}
*/
